/**
 * 
 */
(function($) {
	var count=0
	
	//改写 ajax函数，增加mask遮罩属性
	$._ajax_=$.ajax;
	$.ajax=function( url, options ) {
		var _op=options;
		if ( typeof url === "object" ) {
			_op = url;
		}
		if(_op.mask){
			if(count==0){
				$.messager.progress();
			}
			count++;
		}
		
		var _success_ = _op.success;
		var success = function(data, textStatus, jqXHR){
			if(_op.mask){
				count--;
				if(count==0){
					$.messager.progress("close");
				}
			}
			if (data && data.sucesse != null) {
				if (data.sucesse == "false" || data.sucesse == false) {
					if (data.message) {
						$.messager.alert('错误', data.message, 'error');
					} else if (data.location) {
						var _win = frame.window.top();
						_win.location.pathname = data.location;
					} else {
						$.messager.alert('错误', '发生错误!', 'error');
					}
					return false;
				} else {
					if (data.message) {
						$.messager.alert('提示', data.message, 'info');
					}
				}
			}			
			if($.type(_success_) === "function"){
				_success_(data, textStatus, jqXHR);
			}
		}
		_op.success = success;
		
		var _error_ = _op.error;
		var error = function(data, textStatus, jqXHR){
			if(_op.mask){
				count--;
				if(count==0){
					$.messager.progress("close");
				}
			}		
			if($.type(_error_) === "function"){
				_error_(data, textStatus, jqXHR);
			}
		}
		return $._ajax_( url, options );
	}
	
	$.ajaxSetup({
		timeout : 6000000,
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			$.messager.progress("close");
			alert(XMLHttpRequest+textStatus+errorThrown);
		}
	});
	
	$.extend($.fn, {
		serializeJson: function(field){
	        var formArray = this.serializeArray();
	        var oRet = {};
	        for (var i in formArray) {
	            if (typeof(oRet[formArray[i].name]) == 'undefined') 
	            	oRet[formArray[i].name] = formArray[i].value;
	            else  oRet[formArray[i].name] += "," + formArray[i].value;
	        }
	        if(field) return oRet[field];
	        return oRet;
	    }
	});

})(jQuery);
